package com.jingyanzi.sys.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.sys.dao.SysDictionariesMapper;
import com.jingyanzi.sys.dao.SysNameMappingMapper;
import com.jingyanzi.sys.dao.SysSettingMapper;
import com.jingyanzi.sys.domain.DictionariesDO;
import com.jingyanzi.sys.domain.NameMappingDO;
import com.jingyanzi.sys.domain.SettingDO;
import com.jingyanzi.sys.domain.query.DictionariesQueryDO;
import com.jingyanzi.sys.domain.query.NameMappingQueryDO;
import com.jingyanzi.sys.domain.result.ResultDO;
import com.jingyanzi.sys.domain.result.ResultSupport;
import com.jingyanzi.sys.domain.service.ISettingService;
import com.jingyanzi.sys.entity.SysDictionaries;
import com.jingyanzi.sys.entity.SysDictionariesExample;
import com.jingyanzi.sys.entity.SysDictionariesExample.Criteria;
import com.jingyanzi.sys.entity.SysNameMapping;
import com.jingyanzi.sys.entity.SysNameMappingExample;
import com.jingyanzi.sys.entity.SysSetting;
import com.jingyanzi.sys.entity.SysSettingExample;
import com.jingyanzi.sys.util.BeanUtilsExtends;

@Service("iSettingService")
public class SettingServiceImpl extends BaseServiceImpl implements ISettingService {

	@Autowired(required = true)
	private SysDictionariesMapper dictionariesMapper;

	@Autowired(required = true)
	private SysNameMappingMapper nameMappingMapper;

	@Autowired(required = true)
	private SysSettingMapper settingMapper;
	

	@Override
	public ResultDO addDictionaries(DictionariesDO dictionarieDO) {

		SysDictionaries dictionaries = new SysDictionaries();

		ResultSupport result = BeanUtilsExtends.copy(dictionaries, dictionarieDO);

		if (!result.isSuccess()) {
			return result;
		}
		SysDictionaries sysDictionaries = dictionariesMapper.selectByPrimaryKey(dictionarieDO.getDictionariesId());
		if (sysDictionaries != null) {
			dictionaries.setCode(sysDictionaries.getCode());
		}
		dictionaries.setCreatetime(Calendar.getInstance().getTime());
		dictionaries.setModifitime(dictionaries.getCreatetime());
		dictionaries.setIsDelete(1);
		dictionaries.setStatus(1);
		int r = -1;
		r = dictionariesMapper.insert(dictionaries);
		if (r < 1) {
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, dictionaries.getId());
		return result;
	}

	@Override
	public ResultDO getDictionariesById(Integer id) {

		ResultSupport result = new ResultSupport();

		if (id < 1) {
			result.setSuccess(false);
			return result;
		}

		SysDictionaries dictionaries = null;

		try {
			dictionaries = dictionariesMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (dictionaries == null) {
			result.setSuccess(false);
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getDictionariesDO(dictionaries));

		return result;
	}

	@Override
	public ResultDO removeDictionaries(Integer id) {

		ResultSupport result = new ResultSupport();

		SysDictionaries dictionaries = new SysDictionaries();

		dictionaries.setId(id);
		dictionaries.setIsDelete(-1);

		int r = -1;

		try {
			r = dictionariesMapper.updateByPrimaryKeySelective(dictionaries);
		} catch (Exception e) {

		}

		if (r < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	// isNode 1 是子节点 0是父节点
	@Override
	public ResultDO queryDictionaries(DictionariesQueryDO query) {

		ResultSupport result = new ResultSupport(true);

		if (query == null || query.getIsNode() == null) {

			result.setSuccess(false);
			result.setErrorMsg("请制定是否是子节点");
			return result;
		}
		SysDictionariesExample example = new SysDictionariesExample();

		Criteria createCriteria = example.createCriteria();

		createCriteria.andIsDeleteEqualTo(1);

		createCriteria.andIsNodeEqualTo(query.getIsNode());

		List<DictionariesDO> dictionariesDOs = null;

		if (query.getDictionariesId() != null && query.getDictionariesId() != 0) {
			createCriteria.andDictionariesIdEqualTo(query.getDictionariesId());
		}
		if (query.getCode() != null && query.getCode() != "") {
			createCriteria.andCodeEqualTo(query.getCode());
		}
		if(StringUtils.isNotEmpty(query.getDataItem())) {
			createCriteria.andDataItemLike("%" + query.getDataItem() + "%").andDictionariesIdEqualTo(0);
		}
		// 是否进行分页
		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = dictionariesMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		List<SysDictionaries> selectByExample = null;
		try {
			selectByExample = dictionariesMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		dictionariesDOs = getDictionariesDOList(selectByExample);
		result.setSuccess(true);
		result.setModel(ResultDO.FIRST_MODEL_KEY, dictionariesDOs);
		return result;
	}

	// 递归获取
	public String recursion(int i) {

		SysDictionariesExample example = new SysDictionariesExample();
		example.createCriteria().andDictionariesIdEqualTo(i);
		List<SysDictionaries> list = dictionariesMapper.selectByExample(example);

		String x = "";

		if (list == null || list.size() == 0) {

			return "";
		} else {
			for (SysDictionaries p : list) {
				x = x + "," + p.getId() + "," + recursion(p.getId());
			}
		}
		return x;
	}

	@Override
	public ResultDO addNameMapping(NameMappingDO nameMappingDO) {

		SysNameMapping nameMapping = new SysNameMapping();

		ResultSupport result = BeanUtilsExtends.copy(nameMapping, nameMappingDO);

		if (!result.isSuccess()) {
			return result;
		}

		int r = -1;

		r = nameMappingMapper.insert(nameMapping);
		if (r < 1) {
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, nameMapping.getId());
		return result;
	}

	@Override
	public ResultDO getNameMapping(Integer id) {

		ResultSupport result = new ResultSupport();

		if (id < 1) {
			result.setSuccess(false);
			return result;
		}

		SysNameMapping nameMapping = null;

		try {
			nameMapping = nameMappingMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (nameMapping == null) {
			result.setSuccess(false);
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getNameMappingDO(nameMapping));

		return result;
	}

	@Override
	public ResultDO getNameMappingByNameCode(String nameCode) {

		ResultSupport result = new ResultSupport();

		if (nameCode == null) {
			result.setSuccess(false);
			return result;
		}

		List<SysNameMapping> list = null;

		SysNameMappingExample example = new SysNameMappingExample();

		example.createCriteria().andNameCodeEqualTo(nameCode);

		try {
			list = nameMappingMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (list == null) {
			result.setSuccess(false);
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getNameMappingDO(list.get(0)));

		return result;
	}

	@Override
	public ResultDO modifiNameMapping(NameMappingDO nameMappingDO) {

		SysNameMapping nameMapping = new SysNameMapping();

		ResultSupport result = BeanUtilsExtends.copy(nameMapping, nameMappingDO);
		if (!result.isSuccess())
			return result;

		int r = -1;
		try {
			r = nameMappingMapper.updateByPrimaryKeySelective(nameMapping);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (r < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO removeNameMapping(Integer id) {

		ResultSupport result = new ResultSupport();

		if (!result.isSuccess())
			return result;

		int r = -1;
		try {
			r = nameMappingMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (r < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO queryNameMapping(NameMappingQueryDO query) {

		ResultSupport result = new ResultSupport();

		List<SysNameMapping> list = null;

		SysNameMappingExample example = new SysNameMappingExample();

		if (StringUtils.isNotBlank(query.getNameCode())) {
			example.createCriteria().andNameCodeEqualTo(query.getNameCode());
		}

		List<NameMappingDO> nameMappingDOList = null;

		if (query.isPage()) {
			example.setLimitByPage(query.getStart() + "," + query.getPageRows());
			long count = nameMappingMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = nameMappingMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		nameMappingDOList = getNameMappingDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, nameMappingDOList);

		return result;
	}

	@Override
	public ResultDO addSetting(SettingDO settingDO) {

		SysSetting setting = new SysSetting();

		ResultSupport result = BeanUtilsExtends.copy(setting, settingDO);

		if (!result.isSuccess()) {
			return result;
		}

		setting.setIsSwitch(1);
		int r = -1;

		r = settingMapper.insert(setting);
		if (r < 1) {
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, setting.getId());
		return result;
	}
	
	@Override
	public ResultDO editSetting(SettingDO settingDO) {
		
		SysSetting setting = new SysSetting();
		
		ResultSupport result = BeanUtilsExtends.copy(setting, settingDO);
		
		if (!result.isSuccess()) {
			return result;
		}
		
		setting.setIsSwitch(1);
		int r = -1;
		
		r = settingMapper.updateByPrimaryKeySelective(setting);
		if (r < 1) {
			result.setSuccess(false);
			return result;
		}
		
		result.setModel(ResultDO.FIRST_MODEL_KEY, setting.getId());
		return result;
	}

	@Override
	public ResultDO getSetting(Integer id) {

		ResultSupport result = new ResultSupport();

		if (id < 1) {
			result.setSuccess(false);
			return result;
		}

		SysSetting setting = null;

		try {
			setting = settingMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (setting == null) {
			result.setSuccess(false);
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getSettingDO(setting));

		return result;
	}

	@Override
	public ResultDO getSettingByCode(String code) {
		ResultSupport result = new ResultSupport();

		if (null == code) {
			result.setSuccess(false);
			return result;
		}

		List<SysSetting> settingList = new ArrayList<SysSetting>();
		SysSettingExample example = new SysSettingExample();
		SysSettingExample.Criteria criteria = example.createCriteria();
		criteria.andCodeEqualTo(code);
		try {
			settingList = settingMapper.selectByExample(example);
		} catch (Exception e) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR + "数据查询失败");
			result.setSuccess(false);
			return result;
		}

		if (settingList == null || settingList.size() == 0) {
			result.setErrorCode(ResultDO.DB_NO_DATA);
			result.setErrorMsg(ResultDO.DB_NO_DATA + "数据未找到");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultSupport.FIRST_MODEL_KEY, getSettingDO(settingList.get(0)));
		return result;
	}

	@Override
	public ResultDO removeSetting(Integer id) {

		ResultSupport result = new ResultSupport();

		if (!result.isSuccess())
			return result;

		int r = -1;
		try {
			r = settingMapper.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (r < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO getSettings() {

		ResultSupport result = new ResultSupport();

		List<SysSetting> list = null;

		List<SettingDO> settingDOList = null;

		SysSettingExample example = new SysSettingExample();

		try {
			list = settingMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		settingDOList = getSettingDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, settingDOList);

		return result;
	}

	@Override
	public ResultDO updateDictionaries(DictionariesDO dictionarieDO) {

		ResultDO result = new ResultSupport(true);

		SysDictionaries dictionarie = new SysDictionaries();

		try {
			BeanUtilsExtends.copyProperties(dictionarie, dictionarieDO);
			dictionarie.setModifitime(new Date());
			dictionariesMapper.updateByPrimaryKeySelective(dictionarie);
		} catch (Exception e) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO dictionariesList(DictionariesQueryDO query) {
		ResultSupport result = new ResultSupport(true);

		if (query == null || query.getCode() == null) {

			result.setSuccess(false);
			return result;
		}
		SysDictionariesExample example = new SysDictionariesExample();

		Criteria createCriteria = example.createCriteria();

		createCriteria.andIsDeleteEqualTo(1);
		createCriteria.andIsNodeEqualTo(1);
		List<DictionariesDO> dictionariesDOs = null;

		if (query.getCode() != null && query.getCode() != "") {
			createCriteria.andCodeEqualTo(query.getCode());
		}
		List<SysDictionaries> selectByExample = null;
		try {
			selectByExample = dictionariesMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		dictionariesDOs = getDictionariesDOList(selectByExample);
		result.setModel(ResultDO.FIRST_MODEL_KEY, dictionariesDOs);
		return result;
	}

}
